package com.newtype.meeting.server.service;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.newtype.meeting.client.model.MeetingCategory;
import com.newtype.meeting.client.service.MeetingCategoryService;
import com.newtype.meeting.server.PMF;

/**
 * 会议类别业务逻辑方法实现类
 * @author 汪恭泽
 * Jun 13, 2011 10:41:29 AM
 */
public class MeetingCategoryServiceImpl extends RemoteServiceServlet implements
		MeetingCategoryService {

	private static final long serialVersionUID = 5516651159768135632L;

	/** 删除会议类别 */
	public void deleteCategory(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			MeetingCategory toDeleteCategory = pm.getObjectById(
					MeetingCategory.class, id);
			pm.deletePersistent(toDeleteCategory);
		} finally {
			pm.close();
		}
	}

	/** 根据ID获取会议类别信息 */
	public MeetingCategory getCategory(String id) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm.getObjectById(MeetingCategory.class, id);
	}

	/** 保存会议类别信息 */
	public void saveCategory(MeetingCategory category) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
			pm.makePersistent(category);
		} finally {
			pm.close();
		}
	}

	@SuppressWarnings("unchecked")
	public ArrayList<MeetingCategory> getMainCategories() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingCategory> toReturn;
		try {
			Query query = pm
					.newQuery(MeetingCategory.class, "parentId == null");
			List<MeetingCategory> results = (List<MeetingCategory>) query
					.execute();
			toReturn = new ArrayList<MeetingCategory>();
			toReturn = (List<MeetingCategory>) pm.detachCopyAll(results);
			for (MeetingCategory category : toReturn) {
				category.updateProperties();
			}
		} finally {
			pm.close();
		}
		return (ArrayList<MeetingCategory>) toReturn;
	}

	/** 一级会议类别 */
	@SuppressWarnings("unchecked")
	public List<MeetingCategory> listCategory() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingCategory> toReturn = new ArrayList<MeetingCategory>();
		try {
			Query query = pm
					.newQuery(MeetingCategory.class, "parentId == null");
			List<MeetingCategory> result = (List<MeetingCategory>) query
					.execute();
			toReturn = (List<MeetingCategory>) pm.detachCopyAll(result);
			for (MeetingCategory category : toReturn) {
				category.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/** 下级会议类别 */
	@SuppressWarnings("unchecked")
	public List<MeetingCategory> listChildCategory(MeetingCategory category) {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingCategory> toReturn = new ArrayList<MeetingCategory>();
		try {
			Query query = pm.newQuery(MeetingCategory.class,
					"parentId == keyword");
			query.declareParameters("String keyword");
			List<MeetingCategory> result = (List<MeetingCategory>) query
					.execute(category.getId());
			toReturn = (List<MeetingCategory>) pm.detachCopyAll(result);
			for (MeetingCategory mc : toReturn) {
				mc.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReturn;
	}

	/**
	 * 获得会议类别树形结构
	 */
	public MeetingCategory getCategoryTree() {
		List<MeetingCategory> cates = getAllCategory();
		MeetingCategory root = new MeetingCategory(null, "ROOT");
		for (int i = 0; i < cates.size(); i++) {
			root.add(cates.get(i));
		}
		return root;
	}

	private List<MeetingCategory> getAllCategory() {
		//获取一级类别集合
		List<MeetingCategory> list = listCategory();
		for (MeetingCategory mc : list) {
			setChildren(mc);
		}
		return list;
	}

	/**
	 * 设置子集合
	 * @param category
	 */
	private void setChildren(MeetingCategory category) {
		//取得下级类别集合
		List<MeetingCategory> allChildren = listChildCategory(category);
		if (allChildren != null && allChildren.size() > 0) {
			category.setCategoryChildren(allChildren);
			for (MeetingCategory mc : allChildren) {
				setChildren(mc);
			}
		}
	}

	/**
	 * 获取所有下级菜单
	 */
	@SuppressWarnings("unchecked")
	public List<MeetingCategory> listAllChildren() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		List<MeetingCategory> toReutrn = new ArrayList<MeetingCategory>();
		try {
			Query query = pm.newQuery(MeetingCategory.class, "parentId !=null");
			List<MeetingCategory> result = (List<MeetingCategory>) query.execute();
			toReutrn=(List<MeetingCategory>) pm.detachCopyAll(result);
			for(MeetingCategory category:toReutrn){
				category.updateProperties();
			}
		} finally {
			pm.close();
		}
		return toReutrn;
	}

}
